package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;
import dao.OrderDAO;
import entity.Item;
import entity.Order;
import entity.ReceiveAddress;

public class JdbcOrderDAO implements OrderDAO{
	@Override
	public void saveAddress(ReceiveAddress address) throws Exception {
		Connection conn = DBUtil.getConnction();
		String sql = "insert into d_receive_address (user_id,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?)";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, address.getUserId());
		pst.setString(2, address.getReceiveName());
		pst.setString(3, address.getFullAddress());
		pst.setString(4, address.getPostalCode());
		pst.setString(5, address.getMobile());
		pst.setString(6, address.getPhone());
		pst.executeUpdate();
	}
	@Override
	public List<ReceiveAddress> findByUserId(int userId) throws Exception {
		List<ReceiveAddress> addrs = new ArrayList<ReceiveAddress>();
		Connection conn = DBUtil.getConnction();
		String sql = "select * from d_receive_address where user_id=?";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, userId);
		ResultSet rs = pst.executeQuery();
		while(rs.next()){
			ReceiveAddress addr = new ReceiveAddress();
			addr.setId(rs.getInt("id"));
			addr.setMobile(rs.getString("mobile"));
			addr.setFullAddress(rs.getString("full_address"));
			addr.setPhone(rs.getString("phone"));
			addr.setPostalCode(rs.getString("postal_code"));
			addr.setUserId(rs.getInt("user_id"));
			addr.setReceiveName(rs.getString("receive_name"));
			addrs.add(addr);
		}
		return addrs;
	}
	@Override
	public ReceiveAddress findById(int id) throws Exception {
		ReceiveAddress addr = null;
		Connection conn = DBUtil.getConnction();
		String sql = "select * from d_receive_address where id=?";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, id);
		ResultSet rs = pst.executeQuery();
		while(rs.next()){
			addr = new ReceiveAddress();
			addr.setId(rs.getInt("id"));
			addr.setMobile(rs.getString("mobile"));
			addr.setFullAddress(rs.getString("full_address"));
			addr.setPhone(rs.getString("phone"));
			addr.setPostalCode(rs.getString("postal_code"));
			addr.setUserId(rs.getInt("user_id"));
			addr.setReceiveName(rs.getString("receive_name"));
		}
		return addr;
	}
	@Override
	public void saveItem(Item item) throws Exception {
		Connection conn = DBUtil.getConnction();
		String sql = "insert into d_item (id,order_id,product_id,product_name,dang_price,product_num,amount) values(?,?,?,?,?,?,?)";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, item.getId());
		pst.setInt(2, item.getOrderId());
		pst.setInt(3, item.getProductId());
		pst.setString(4, item.getProductName());
		pst.setDouble(5, item.getDangPrice());
		pst.setInt(6, item.getProductNum());
		pst.setDouble(7, item.getAmount());
		pst.executeUpdate();
	}
	@Override
	public int saveOrder(Order order) throws Exception {
		Connection conn = DBUtil.getConnction();
		String sql = "insert into d_order(user_id,status,order_time,order_desc,total_price,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pst = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
		pst.setInt(1, order.getUserId());
		pst.setLong(2, order.getStatus());
		pst.setLong(3, order.getOrderTime());
		pst.setString(4, order.getOrderDesc());
		pst.setDouble(5, order.getTotalPrice());
		pst.setString(6, order.getReceiveName());
		pst.setString(7, order.getFullAddress());
		pst.setString(8, order.getPostalCode());
		pst.setString(9, order.getMobile());
		pst.setString(10, order.getPhone());
		pst.executeUpdate();
		ResultSet rs = pst.getGeneratedKeys();
		rs.next();
		return rs.getInt(1);
	}

	@Override
	public int findMaxId() throws Exception {
		int result=0;
		Connection conn = DBUtil.getConnction();
		PreparedStatement pst = conn.prepareStatement("select max(id) as maxId from d_order");
		ResultSet rs = pst.executeQuery();
		if(rs.next()){
			result=rs.getInt("maxId");
		}
		return result;
	}
	@Override
	public boolean findAddressByName(String name) throws Exception {
		Connection conn = DBUtil.getConnction();
		PreparedStatement pst = conn.prepareStatement("select count(*) as sum from d_receive_address where receive_name=?");
		pst.setString(1, name);
		ResultSet rs = pst.executeQuery();
		return rs.next();
	}
	@Override
	public ReceiveAddress findByFullAddress(String address) throws Exception {
		ReceiveAddress addr = null;
		Connection conn = DBUtil.getConnction();
		String sql = "select * from d_receive_address where full_address=?";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, address);
		ResultSet rs = pst.executeQuery();
		while(rs.next()){
			addr = new ReceiveAddress();
			addr.setId(rs.getInt("id"));
			addr.setMobile(rs.getString("mobile"));
			addr.setFullAddress(rs.getString("full_address"));
			addr.setPhone(rs.getString("phone"));
			addr.setPostalCode(rs.getString("postal_code"));
			addr.setUserId(rs.getInt("user_id"));
			addr.setReceiveName(rs.getString("receive_name"));
		}
		return addr;
	}
	@Override
	public void updateAddress(ReceiveAddress address) throws Exception {
		Connection conn = DBUtil.getConnction();
		String sql ="update d_receive_address set phone=?,mobile=?,postal_code=?,receive_name=? where full_address=?";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, address.getPhone());
		pst.setString(2, address.getMobile());
		pst.setString(3, address.getPostalCode());
		pst.setString(4, address.getReceiveName());
		pst.setString(5, address.getFullAddress());
		pst.executeUpdate();
	}
}
